package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.ClueDto;
import com.qk.dto.MarkFalseClueDto;
import com.qk.entity.Clue;
import com.qk.entity.ClueTrackRecord;
import com.qk.mapper.ClueTrackRecordMapper;
import com.qk.service.ClueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/clues")
public class ClueController {
    @Autowired
    private ClueService clueService;


    @PostMapping
    public Result add(@RequestBody Clue clue){
        clue.setCreateTime(LocalDateTime.now());
        clue.setUpdateTime(LocalDateTime.now());
        log.info("添加线索" +clue);
        clueService.save(clue);
        return Result.success();
    }

    /**
     * 获取线索列表
     */
    @GetMapping
    public Result listClues(ClueDto clueDto){
        PageResult<Clue> pageResult =clueService.listClues(clueDto);
        return Result.success(pageResult);
    }
    /**
     * 分配线索
     */
    @PutMapping("/assign/{clueId}/{userId}")
    public Result assignClue(@PathVariable Integer clueId,@PathVariable Integer userId){
        Clue clue = new Clue();
        clue.setId(clueId);
        clue.setUserId(userId);
        clue.setStatus(2);
        clue.setUpdateTime(LocalDateTime.now());
        clueService.updateById(clue);
        return Result.success();
    }

    /**
     * 根据Id查询
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
       Clue clue = clueService.getClueById(id);
       return Result.success(clue);
    }

    /**
     * 跟进线索
     */
    @PutMapping
    public Result trackClue(@RequestBody Clue  clue){
        clueService.trackClue(clue);
        return Result.success();
    }

    /**
     * 伪线索处理
     */
    @PutMapping("/false/{id}")
    public Result falseClue(@PathVariable Integer id, @RequestBody MarkFalseClueDto markFalseClueDto){
        clueService.updateFalseClue(id,markFalseClueDto);
        return Result.success();
    }

    /**
     * 转商机
     */
    @PutMapping("/toBusiness/{id}")
    public Result toBusiness(@PathVariable Integer id){
        log.info("转商机 {}" ,id );
        clueService.toBusiness(id);
        return Result.success();
    }


    /**
     * 线索池列表查询
     */
    @GetMapping("/pool")
    public Result listPoolClues(ClueDto clueDto){
       PageResult<Clue> pageResult = clueService.listPoolClues(clueDto);
       return Result.success(pageResult);
    }

}
